package cn.edu.ctbu.datajpa01.restcontroller;

import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.helper.PageUtils;
import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import cn.edu.ctbu.datajpa01.helper.R;
import org.springframework.data.domain.Pageable;
import java.util.List;


@RestController
@RequestMapping(value = "/webapi/student")
public class StudentRestController {
    @Autowired
    StudentService studentService;

    @GetMapping("list")
    public List<Student> GetAll(){
        return studentService.findAll();
    }
    @GetMapping("newlist")
    public PageUtils GetAll(Integer pageSize, Integer pageNum){
        Pageable pageable= PageRequest.of(pageNum-1,pageSize);
        Page<Student> tempStudents=studentService.findAll(pageable);
        PageUtils pageUtils=new PageUtils(tempStudents.getTotalElements(),tempStudents.getContent());
        return pageUtils;
    }

    @GetMapping(value = "{id}")
    public Student GetById(@PathVariable Integer id){return studentService.findById(id);}

    @GetMapping(value = "name/{name}")
    public Student GetByName(@PathVariable String name){
        List<Student> students=studentService.findAll();//获取所有学生信息
        for (int i = 0; i < students.size(); i++) {
            String tempnmae=String.valueOf(students.get(i).getName());//获取每个学生姓名，并于name对比
            if (name.equals(tempnmae)) {
                return students.get(i);//找到就返回对应学生对象
            }
        }
        return null;//没找到
    }

    @DeleteMapping(value = "delete/{id}")
    public  boolean DeleteById(@PathVariable Integer id){studentService.delete(id);return  true;}
    @PostMapping(value = "add")
    public Integer Add( Student student){
        studentService.save(student);
        return student.getId();}
    @PutMapping(value = "upData")
    public Student Updata( Student student){
        //读取旧的数据
        Student oldstudent=studentService.findById(student.getId());
        studentService.upData(student);
        if (StringUtils.hasText(student.getPassword())){
            //没有新密码，就用旧密码
            student.setPassword(oldstudent.getPassword());

        }
        return student;}

    @PostMapping("insert")
    public Student insert( Student student){
        Student student1=studentService.insert(student);
        return student1;}

    @GetMapping("/get/{id}")
    public  Student get(@PathVariable Integer id){
        Student student=studentService.findById(id);
        return student;
    }


    @GetMapping("/getByNo/{no}")
    public Student getByNo(@PathVariable Long no) {
        Student student = studentService.findByNo(no);
        return student;
    }

    @GetMapping("/getByName/{name}")
    public List<Student> getByName(@PathVariable String name) {
        List<Student> student = studentService.findByName(name);
        return student;
    }
    @GetMapping(value = "login/{userName} {password}")
    public  R login(@PathVariable String userName,@PathVariable String password) throws Exception {
        studentService.validByNameAndPassword(userName,password);
        return R.ok();
    }
}
